CC  = gcc
F90 = gfortran
PYTHON = python
PYQTUIC = pyside-uic
# F90FLAG = -fPIC -shared -openmp

NAME = numext

ifeq ($(OS),Windows_NT)
	COMPILER = msvc
	BLASLAPACK=libblas.dll liblapack.dll
	FFTW=libfftw3-3.dll
	OBJ_EXT=obj
	DLL_EXT=dll
	DEP_LIB=$(NAME).lib
	F90FLAG = -shared -O3
	F2PY = f2py
	F2PYFLAG = --quiet -L. --compiler=$(COMPILER) -lblas -llapack -lfftw3-3 -lgfortran-3 -l$(NAME)
else
	BLASLAPACK = -lblas -llapack
	FFTW = -lfftw3
	OBJ_EXT =o
	DEP_LIB =
	ifeq ($(shell uname),Darwin)
		DLL_EXT=dylib
		F90FLAG = -fPIC -shared -O3 -L. $(BLASLAPACK) $(FFTW)
		F2PY = f2py
		F2PYFLAG = --quiet -L. $(BLASLAPACK) $(FFTW) -l$(NAME)
	else
		DLL_EXT=so
		F90FLAG = -fPIC -shared -O3 -L. $(BLASLAPACK) $(FFTW)
		F2PY = $(PYTHON) $(shell which f2py)
		F2PYFLAG = --quiet -L. $(BLASLAPACK) $(FFTW) -l$(NAME)
	endif
endif

DYLIB = lib$(NAME).$(DLL_EXT)
OBJ = $(NAME).$(OBJ_EXT)

all: f2py pyui

lib: $(DYLIB) $(DEP_LIB)

dylib: $(DYLIB)

$(DYLIB): $(OBJ)
	$(F90) $(OBJ) $(F90FLAG) -L. $(BLASLAPACK) $(FFTW) -o $(DYLIB)

%.$(OBJ_EXT):%.f90
	$(F90) -c $< $(F90FLAG) -o $@

f2py: $(DYLIB) $(DEP_LIB)
	$(F2PY) -c -m utils utils.f90 $(F2PYFLAG)

%.pyd:%.f90
	$(PYQTUIC) $< -o $@

pyui: EQSignal_UI_D.py About_D.py EQSignal_UI_M_D.py

%.py:%.ui
	$(PYQTUIC) $< -o $@

%.lib:lib%.dll
	$(PYTHON) dll2lib.py $<

install: $(DYLIB)
	cp $< /usr/lib

uninstall:
	rm -vf /usr/lib/lib$(NAME).$(DLL_EXT)

.PHONY : clean, clear, test

clean:
	rm -vf $(DYLIB) $(OBJ) *.mod $(DEP_LIB) utils.pyd *.pyc utils.so

clear:
	rm -vf $(OBJ) *.mod

test: pyui
	$(PYTHON) run.py
